export const isObject = function (x) {
  const type = typeof x;
  return x !== null && (type === "object" || type === "function");
};


export const formateDate = function () {
  let date = new Date();
  let seperator1 = "/";
  let year = date.getFullYear();//年
  let month = date.getMonth() + 1;//月
  let strDate = date.getDate(); //日
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  return year + seperator1 + month + seperator1 + strDate;
};
export const normalizeHeaderName = (headers, normalizedName) => {
  if (!headers) {
    return;
  }
  Object.keys(headers).forEach(name => {
    if (
      name !== normalizedName &&
      name.toUpperCase() === normalizedName.toUpperCase()
    ) {
      headers[normalizedName] = headers[name];
      delete headers[name];
    }
  });
};


export const createFakeIp = () => {
  let ip = [];
  for (let i = 0; i < 4; i++) {
    ip.push(Math.round(Math.random() * (255 - 1)) + 1);
  }
  return ip.join(".");
};


export const isFullScreenPhone = () => {
  let result = false;
  const rate = window.screen.height / window.screen.width;
  let limit = window.screen.height === window.screen.availHeight ? 1.8 : 1.65; // 临界判断值
  // window.screen.height为屏幕高度
  //  window.screen.availHeight 为浏览器 可用高度
  if (rate > limit) {
    result = true;
  }
  return result;
};


export const hasLogin = () => {
  return localStorage.getItem('token') !== null
    && localStorage.getItem('token') !== ''
    && localStorage.getItem('token') !== 'undefined';
};


export const getUrlParam = (name) => {
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  let r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
};
export const dataURItoBlob = (dataurl) => {
  let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], {type: mime});
};



export function debounce(func,wait,immediate) {
  let timeout;
  return function () {
    let context = this;
    let args = arguments;

    if (timeout) clearTimeout(timeout);
    if (immediate) {
      let callNow = !timeout;
      timeout = setTimeout(() => {
        timeout = null;
      }, wait);
      if (callNow) func.apply(context, args)
    }
    else {
      timeout = setTimeout(() => {
        func.apply(context, args)
      }, wait);
    }
  }
}

